In [1]:
import sympy

What is the area of a circle of radius 3?


In [3]:
A='9*pi'

print('The answer is: %0.2f' % sympy.sympify(A).evalf())


The answer is: 28.27

What is the antiderivative of $x^2+4x$? (write as a function of the indeterminate x)


In [19]:
A='2*x^2+x^3/3'
X=sympy.Symbol('x')
correct_answer = X**3/3+2*X**2
correct_answer


Out[19]:
x**3/3 + 2*x**2

In [20]:
print('My answer is: %s' % A)
print('The assignment answer is %s' % repr(correct_answer))
print(correct_answer == sympy.sympify(A))


My answer is: 2*x^2+x^3/3
The assignment answer is x**3/3 + 2*x**2
True

It also means you don't have to escape anything because it doesn't do python evals


In [21]:
sympy.sympify('os.system("ls")')


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-21-4b5a07ce8261> in <module>()
----> 1 sympy.sympify('os.system("ls")')

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/sympy/core/sympify.py in sympify(a, locals, convert_xor, strict, rational)
    153 
    154     try:
--> 155         expr = parse_expr(a, locals or {}, rational, convert_xor)
    156     except (TokenError, SyntaxError):
    157         raise SympifyError('could not parse %r' % a)

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/sympy/parsing/sympy_parser.py in parse_expr(s, local_dict, rationalize, convert_xor)
    109 
    110     code = _transform(s.strip(), local_dict, global_dict, rationalize, convert_xor)
--> 111     expr = eval(code, global_dict, local_dict) # take local objects in preference
    112 
    113     return expr

<string> in <module>()

AttributeError: 'Symbol' object has no attribute 'Symbol'

In [ ]: